//
//  SAAddModifyStaffCell.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/9.
//

#import "SAAddModifyStaffCell.h"

@interface SAAddModifyStaffCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIView *lineView;



@end

@implementation SAAddModifyStaffCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)refreshCellWithTitle:(NSString *)title placeHolder:(NSString *)placeHolder content:(NSString *)content {
    
    if ([title containsString:@"*"]) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:[title rangeOfString:@"*"]];
        self.titleLab.attributedText = attributedString;
        
    } else {
        self.titleLab.text = title;
    }
    
    self.textField.placeholder = placeHolder;
    self.textField.text = content;
    
    if ([title isEqualToString:@"岗位*"] || [title isEqualToString:@"店铺*"]) {
        self.imageV.hidden = NO;
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(kScaleNum(-40));
        }];
        self.textField.userInteractionEnabled = NO;
        self.typeBtn.hidden = NO;
    }else {
        self.imageV.hidden = YES;
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(kScaleNum(-25));
        }];
        self.typeBtn.hidden = YES;
        
        if (self.isBoss == YES && [title isEqualToString:@"手机*"]) {
            self.textField.userInteractionEnabled = NO;
            self.textField.textColor = kLightgGrayColor;
        }else {
            self.textField.userInteractionEnabled = YES;
            self.textField.textColor = kBlackColor;
        }
    }
    
    if ([title isEqualToString:@"密码*"]) {
        self.textField.secureTextEntry = YES;
    }else {
        self.textField.secureTextEntry = NO;
    }
    
    if ([title isEqualToString:@"手机*"]) {
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
    }else {
        self.textField.keyboardType = UIKeyboardTypeDefault;
    }
}

- (void)refreshManagementCellWithTitle:(NSString *)title placeHolder:(NSString *)placeHolder content:(NSString *)content {
    if ([title containsString:@"*"]) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:title];
        [attributedString addAttribute:NSForegroundColorAttributeName value:kRedColor range:[title rangeOfString:@"*"]];
        self.titleLab.attributedText = attributedString;
    } else {
        self.titleLab.text = title;
    }
    if ([title isEqualToString:@"联系人*"]) {
        self.textField.userInteractionEnabled = NO;
        self.textField.textColor = kLightgGrayColor;
    }else {
        self.textField.userInteractionEnabled = YES;
        self.textField.textColor = kBlackColor;
    }
    self.textField.placeholder = placeHolder;
    self.textField.text = content;
    self.imageV.hidden = YES;
    self.typeBtn.hidden = YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if ([self.titleLab.text isEqualToString:@"手机*"]) {
        if (textField.text.length > 10) {
            return NO;
        }
    }
    if ([self.titleLab.text isEqualToString:@"密码*"]) {
        if (textField.text.length > 19) {
            return NO;
        }
    }
    return YES;
}

#pragma mark - event
-(void)clickedTypeBtn {
    !self.positionBlock ?: self.positionBlock();
}

#pragma mark - UITextFieldDelegate

- (void)textFieldEditChanged:(UITextField *)textField {
    if (self.textFieldBlock) {
        self.textFieldBlock(self.titleLab.text, self.textField.text);
    }
}

#pragma mark - initUI

- (void)initUI {

    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.textField];
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.image = [UIImage imageNamed:@"Sale_rightArrow"];
    [self.contentView addSubview:imageV];
    self.imageV = imageV;
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.typeBtn];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kScaleNum(25));
        make.centerY.mas_equalTo(0);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(kScaleNum(-25));
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(kScaleNum(130));
    }];
    
    [self.imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(kScaleNum(-25));
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(kScaleNum(8));
        make.height.mas_equalTo(kScaleNum(15));
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    
    [self.typeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.left.mas_equalTo(kScaleNum(130));
    }];
}

#pragma mark - lazy init
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.font = kFont(16);
        _titleLab.textColor = kBlackColor;
        [_titleLab setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    }
    return _titleLab;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.textAlignment = NSTextAlignmentLeft;
        _textField.delegate = self;
        _textField.font = kFont(16);
        _textField.textColor = kBlackColor;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_textField addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kLineColor;
    }
    return _lineView;
}

-(UIButton *)typeBtn {
    if (!_typeBtn) {
        _typeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_typeBtn addTarget:self action:@selector(clickedTypeBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _typeBtn;
}

@end

